#include "block.h"

block::block(int t)
{
	creatblock(t);	
}

block::block(const block & copy)
{
	box=copy.box;
	type=copy.type;
	x=4;
	y=0;
}

block::~block()
{
}

void block::creatblock(int t)
{
	fix_NULL();
	if(t>=0&&t<8)			//8%
		fix_O();
	else if(t>=8&&t<18)		//20%
		fix_I0();
	else if(t>=18&&t<28)
		fix_I1();
	else if(t>=28&&t<33)	//20%
		fix_T0();
	else if(t>=33&&t<38)
		fix_T1();
	else if(t>=38&&t<43)
		fix_T2();
	else if(t>=43&&t<48)
		fix_T3();
	else if(t>=48&&t<52)	//16%
		fix_L0();
	else if(t>=52&&t<56)
		fix_L1();
	else if(t>=56&&t<60)
		fix_L2();
	else if(t>=60&&t<64)
		fix_L3();
	else if(t>=64&&t<68)	//16%
		fix_J0();
	else if(t>=68&&t<72)
		fix_J1();
	else if(t>=72&&t<76)
		fix_J2();
	else if(t>=76&&t<80)
		fix_J3();
	else if(t>=80&&t<85)	//10%
		fix_Z0();
	else if(t>=85&&t<90)
		fix_Z1();
	else if(t>=90&&t<95)	//10%
		fix_S0();
	else if(t>=95&&t<100)
		fix_S1();
	else
		fix_O();
	type=t;
}

void block::fix_NULL()
{
	for(int j=0;j<4;j++)
		for(int i=0;i<4;i++)
			box(i,j)=0;
}

void block::fix_O()
{
	box(0,0)=1;
	box(1,0)=1;
	box(0,1)=1;
	box(1,1)=1;
	x=4;
	y=1;
}

void block::fix_I0()
{
	box(0,0)=2;
	box(0,1)=2;
	box(0,2)=2;
	box(0,3)=2;
	x=4;
	y=0;
}

void block::fix_I1()
{
	box(0,0)=2;
	box(1,0)=2;
	box(2,0)=2;
	box(3,0)=2;
	x=3;
	y=0;
}

void block::fix_T0()
{
	box(0,0)=3;
	box(1,0)=3;
	box(2,0)=3;
	box(1,1)=3;
	x=3;
	y=0;
}

void block::fix_T1()
{
	box(1,0)=3;
	box(0,1)=3;
	box(1,1)=3;
	box(1,2)=3;
	x=4;
	y=0;
}

void block::fix_T2()
{
	box(1,0)=3;
	box(0,1)=3;
	box(1,1)=3;
	box(2,1)=3;
	x=3;
	y=0;
}

void block::fix_T3()
{
	box(0,0)=3;
	box(0,1)=3;
	box(0,2)=3;
	box(1,1)=3;
	x=4;
	y=0;
}

void block::fix_L0()
{
	box(0,0)=4;
	box(0,1)=4;
	box(0,2)=4;
	box(1,2)=4;
	x=4;
	y=0;
}

void block::fix_L1()
{
	box(0,0)=4;
	box(1,0)=4;
	box(2,0)=4;
	box(0,1)=4;
	x=3;
	y=0;
}

void block::fix_L2()
{
	box(0,0)=4;
	box(1,0)=4;
	box(1,1)=4;
	box(1,2)=4;
	x=4;
	y=0;
}

void block::fix_L3()
{
	box(2,0)=4;
	box(0,1)=4;
	box(1,1)=4;
	box(2,1)=4;
	x=3;
	y=0;
}

void block::fix_J0()
{
	box(1,0)=5;
	box(1,1)=5;
	box(1,2)=5;
	box(0,2)=5;
	x=4;
	y=0;
}

void block::fix_J1()
{
	box(0,0)=5;
	box(0,1)=5;
	box(1,1)=5;
	box(2,1)=5;
	x=3;
	y=0;
}

void block::fix_J2()
{
	box(0,0)=5;
	box(0,1)=5;
	box(0,2)=5;
	box(1,0)=5;
	x=4;
	y=0;
}

void block::fix_J3()
{
	box(0,0)=5;
	box(1,0)=5;
	box(2,0)=5;
	box(2,1)=5;
	x=3;
	y=0;
}

void block::fix_Z0()
{
	box(0,0)=6;
	box(1,0)=6;
	box(1,1)=6;
	box(2,1)=6;
	x=3;
	y=0;
}

void block::fix_Z1()
{
	box(1,0)=6;
	box(0,1)=6;
	box(1,1)=6;
	box(0,2)=6;
	x=4;
	y=0;
}

void block::fix_S0()
{
	box(1,0)=7;
	box(2,0)=7;
	box(0,1)=7;
	box(1,1)=7;
	x=3;
	y=0;
}

void block::fix_S1()
{
	box(0,0)=7;
	box(0,1)=7;
	box(1,1)=7;
	box(1,2)=7;
	x=4;
	y=0;
}

piece  block::getbox()
{
	return box;
}

int  block::gettype()
{
	return type;
}

int  block::getx()
{
	return x;
}

int  block::gety()
{
	return y;
}

bool block::turn(piece & GameLocation)
{
	int type_next;

	if(type>=0&&type<8)
		type_next=type;
	else if(type>=8&&type<18)
		type_next=type+10;
	else if(type>=18&&type<28)
		type_next=type-10;
	else if(type>=28&&type<43)
		type_next=type+5;
	else if(type>=43&&type<48)
		type_next=type-15;
	else if((type>=48&&type<60)||(type>=64&&type<76))
		type_next=type+4;
	else if((type>=60&&type<64)||(type>=76&&type<80))
		type_next=type-12;
	else if((type>=80&&type<85)||(type>=90&&type<95))
		type_next=type+5;
	else if((type>=85&&type<90)||(type>=95&&type<100))
		type_next=type-5;


	block block_tmp(type_next);
	if(!GameLocation.lost(block_tmp.box,x,y))
	{
		box=block_tmp.box;
		type=type_next;
		return true;
	}
	else
		return false;
}

bool block::move_lift(piece & GameLocation)
{
	if(!GameLocation.lost(box,x-1,y))
	{
		x=x-1;
		return true;
	}
	else
		return false;
}

bool block::move_right(piece & GameLocation)
{
	if(!GameLocation.lost(box,x+1,y))
	{
		x=x+1;
		return true;
	}
	else
		return false;
}

bool block::move_down(piece & GameLocation)
{
	if(!GameLocation.lost(box,x,y+1))
	{
		y=y+1;
		return true;
	}
	else
		return false;
}

bool block::isTouch(piece & GameLocation)
{
	if(GameLocation.lost(box,x,y+1))
		return true;
	else
		return false;
}

block & block::operator = (block & tar)
{
	if(&tar!=this)
	{
		box=tar.getbox();
		type=tar.gettype();
		x=tar.getx();
		y=tar.gety();
	}
	return *this;
}
